Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_coachRoot
    Inherits Csla.BusinessBase(Of rw_coachRoot)

#Region " Business Properties and Methods "

    ' declare members
    Private _coachId As Integer = 0
    Private _leagueId As Integer = 0
    Private _coachnameFirst As String = String.Empty
    Private _coachnameLast As String = String.Empty
    Private _coachphonePrimary As String = String.Empty
    Private _coachphoneCell As String = String.Empty
    Private _coachEmail As String = String.Empty
    Private _createDate As SmartDate = New SmartDate(DateTime.Today)

    ''' <summary>
    ''' Get the CoachId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the CoachId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, True)> _
    Public ReadOnly Property CoachId() As Integer
        Get
            CanReadProperty("CoachId", True)
            Return _coachId
        End Get
    End Property

    ''' <summary>
    ''' Get or Set the LeagueId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the LeagueId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property LeagueId() As Integer
        Get
            CanReadProperty("LeagueId", True)
            Return _leagueId
        End Get
        Set(ByVal value As Integer)
            CanWriteProperty("LeagueId", True)
            If Not _leagueId.Equals(value) Then
                _leagueId = value
                PropertyHasChanged("LeagueId")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CoachnameFirst of the current object.
    ''' </summary>
    ''' <value>A String that represents the CoachnameFirst of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CoachnameFirst() As String
        Get
            CanReadProperty("CoachnameFirst", True)
            Return _coachnameFirst
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CoachnameFirst", True)
            If value Is Nothing Then value = String.Empty
            If Not _coachnameFirst.Equals(value) Then
                _coachnameFirst = value
                PropertyHasChanged("CoachnameFirst")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CoachnameLast of the current object.
    ''' </summary>
    ''' <value>A String that represents the CoachnameLast of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CoachnameLast() As String
        Get
            CanReadProperty("CoachnameLast", True)
            Return _coachnameLast
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CoachnameLast", True)
            If value Is Nothing Then value = String.Empty
            If Not _coachnameLast.Equals(value) Then
                _coachnameLast = value
                PropertyHasChanged("CoachnameLast")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CoachphonePrimary of the current object.
    ''' </summary>
    ''' <value>A String that represents the CoachphonePrimary of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CoachphonePrimary() As String
        Get
            CanReadProperty("CoachphonePrimary", True)
            Return _coachphonePrimary
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CoachphonePrimary", True)
            If value Is Nothing Then value = String.Empty
            If Not _coachphonePrimary.Equals(value) Then
                _coachphonePrimary = value
                PropertyHasChanged("CoachphonePrimary")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CoachphoneCell of the current object.
    ''' </summary>
    ''' <value>A String that represents the CoachphoneCell of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CoachphoneCell() As String
        Get
            CanReadProperty("CoachphoneCell", True)
            Return _coachphoneCell
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CoachphoneCell", True)
            If value Is Nothing Then value = String.Empty
            If Not _coachphoneCell.Equals(value) Then
                _coachphoneCell = value
                PropertyHasChanged("CoachphoneCell")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CoachEmail of the current object.
    ''' </summary>
    ''' <value>A String that represents the CoachEmail of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CoachEmail() As String
        Get
            CanReadProperty("CoachEmail", True)
            Return _coachEmail
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CoachEmail", True)
            If value Is Nothing Then value = String.Empty
            If Not _coachEmail.Equals(value) Then
                _coachEmail = value
                PropertyHasChanged("CoachEmail")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CreateDate of the current object.
    ''' </summary>
    ''' <value>A DateTime that represents the CreateDate of the current object.</value>
    ''' <returns>DateTime representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CreateDate() As DateTime
        Get
            CanReadProperty("CreateDate", True)
            Return _createDate.Date
        End Get
        Set(ByVal value As DateTime)
            CanWriteProperty("CreateDate", True)
            If Not _createDate.Equals(value) Then
                _createDate.Date = value
                PropertyHasChanged("CreateDate")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CreateDateString of the current object.
    ''' </summary>
    ''' <value>A String that represents the CreateDateString of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CreateDateString() As String
        Get
            CanReadProperty("CreateDateString", True)
            Return _createDate.Text
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CreateDateString", True)
            If value Is Nothing Then value = String.Empty
            If Not _createDate.Equals(value) Then
                _createDate.Text = value
                PropertyHasChanged("CreateDateString")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _coachId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' CoachnameFirst rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "CoachnameFirst")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("CoachnameFirst", 50))
        ' CoachnameLast rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "CoachnameLast")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("CoachnameLast", 50))
        ' CoachphonePrimary rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "CoachphonePrimary")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("CoachphonePrimary", 50))
        ' CoachphoneCell rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("CoachphoneCell", 50))
        ' CoachEmail rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("CoachEmail", 50))
        ' CreateDate rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "CreateDateString")
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in rw_coachRoot
        'AuthorizationRules.AllowRead("CoachId", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("LeagueId", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CoachnameFirst", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CoachnameLast", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CoachphonePrimary", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CoachphoneCell", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CoachEmail", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CreateDate", "rw_coachRootReadGroup")
        'AuthorizationRules.AllowRead("CreateDateString", "rw_coachRootReadGroup")

        'AuthorizationRules.AllowWrite("LeagueId", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CoachnameFirst", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CoachnameLast", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CoachphonePrimary", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CoachphoneCell", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CoachEmail", "rw_coachRootWriteGroup")
        'AuthorizationRules.AllowWrite("CreateDateString", "rw_coachRootWriteGroup")
    End Sub

    Public Shared Function CanGetObject() As Boolean
        'TODO: Define CanGetObject permission in rw_coachRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_coachRootViewGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanAddObject() As Boolean
        'TODO: Define CanAddObject permission in rw_coachRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_coachRootAddGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanEditObject() As Boolean
        'TODO: Define CanEditObject permission in rw_coachRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_coachRootEditGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanDeleteObject() As Boolean
        'TODO: Define CanDeleteObject permission in rw_coachRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_coachRootDeleteGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

    Private Sub New()
        ' require use of factory method 
    End Sub

    Public Shared Function NewCoachRoot(ByVal coachId As Integer) As rw_coachRoot
        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_coachRoot")
        End If
        Return DataPortal.Create(Of rw_coachRoot)(New Criteria(coachId))
    End Function

    Public Shared Function GetCoachRoot(ByVal coachId As Integer) As rw_coachRoot
        If Not CanGetObject() Then
            Throw New System.Security.SecurityException("User not authorized to view a rw_coachRoot")
        End If
        Return DataPortal.Fetch(Of rw_coachRoot)(New Criteria(coachId))
    End Function

    Public Shared Sub Deleterw_coachRoot(ByVal coachId As Integer)
        If Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_coachRoot")
        End If
        DataPortal.Delete(New Criteria(coachId))
    End Sub

    Public Overrides Function Save() As rw_coachRoot
        If IsDeleted AndAlso Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_coachRoot")
        ElseIf IsNew AndAlso Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_coachRoot")
        ElseIf Not CanEditObject() Then
            Throw New System.Security.SecurityException("User not authorized to update a rw_coachRoot")
        End If
        Return MyBase.Save()
    End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Criteria "

    <Serializable()> _
    Private Class Criteria

        Public CoachId As Integer

        Public Sub New(ByVal coachId As Integer)
            Me.CoachId = coachId
        End Sub
    End Class

#End Region 'Criteria

#Region " Data Access - Create "

    <RunLocal()> _
    Protected Overloads Sub DataPortal_Create(ByVal criteria As Object)
        ValidationRules.CheckRules()
    End Sub

#End Region ' Data Access - Create

#Region " Data Access - Fetch "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteFetch(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_getCoachInfo"
            If criteria.CoachId <> 0 Then
                cm.Parameters.AddWithValue("@iFilter", 1)
                cm.Parameters.AddWithValue("@coachId", criteria.CoachId)
            Else
                cm.Parameters.AddWithValue("@iFilter", 0)
                cm.Parameters.AddWithValue("@coachId", 0)
            End If

            Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                FetchObject(dr)
                ValidationRules.CheckRules()

                'load child object(s)
                FetchChildren(dr)
            End Using
        End Using
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        dr.Read()
        _coachId = dr.GetInt32("coachId")
        _leagueId = dr.GetInt32("leagueId")
        _coachnameFirst = dr.GetString("coachName_first")
        _coachnameLast = dr.GetString("coachName_last")
        _coachphonePrimary = dr.GetString("coachPhone_primary")
        _coachphoneCell = dr.GetString("coachPhone_cell")
        _coachEmail = dr.GetString("coachEmail")
        _createDate = dr.GetSmartDate("createDate", _createDate.EmptyIsMin)
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Protected Overrides Sub DataPortal_Insert()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteInsert(cn)

            'update child object(s)
            UpdateChildren(cn)
        End Using
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_insCoachInfo"

            AddInsertParameters(cm)

            cm.ExecuteNonQuery()

            _coachId = CType(cm.Parameters("@new_coachId").Value, Integer)
        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@leagueId", _leagueId)
        cm.Parameters.AddWithValue("@coachName_first", _coachnameFirst)
        cm.Parameters.AddWithValue("@coachName_last", _coachnameLast)
        cm.Parameters.AddWithValue("@coachPhone_primary", _coachphonePrimary)
        cm.Parameters.AddWithValue("@coachPhone_cell", _coachphoneCell)
        cm.Parameters.AddWithValue("@coachEmail", _coachEmail)
        cm.Parameters.AddWithValue("@new_coachId", _coachId)
        cm.Parameters("@new_coachId").Direction = ParameterDirection.Output
    End Sub

#End Region 'Data Access - Insert

#Region " Data Access - Update "

    Protected Overrides Sub DataPortal_Update()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            If MyBase.IsDirty Then
                ExecuteUpdate(cn)
            End If

            ' update child object(s)
            UpdateChildren(cn)
        End Using

    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_updCoachInfo"

            AddUpdateParameters(cm)

            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@coachId", _coachId)
        cm.Parameters.AddWithValue("@leagueId", _leagueId)
        cm.Parameters.AddWithValue("@coachName_first", _coachnameFirst)
        cm.Parameters.AddWithValue("@coachName_last", _coachnameLast)
        cm.Parameters.AddWithValue("@coachPhone_primary", _coachphonePrimary)
        cm.Parameters.AddWithValue("@coachPhone_cell", _coachphoneCell)
        cm.Parameters.AddWithValue("@coachEmail", _coachEmail)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
    End Sub

#End Region 'Data Access - Update

#Region " Data Access - Delete "

    Protected Overrides Sub DataPortal_DeleteSelf()
        DataPortal_Delete(New Criteria(_coachId))
    End Sub

    Private Overloads Sub DataPortal_Delete(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteDelete(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_delCoachInfo"

            cm.Parameters.AddWithValue("@coachId", criteria.CoachId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region 'Data Access


End Class

